//
//  YGServerItemCollectionViewCell.m
//  yuanshangyiliao
//
//  Created by icholab on 2021/4/24.
//

#import "YGServerItemCollectionViewCell.h"
@interface YGServerItemCollectionViewCell ()
@property (nonatomic, strong) UIImageView  *coverImageView;
@property (nonatomic, strong) UILabel  *titleLabel;
@property (nonatomic, strong) UILabel  *nengliangLabel;
@property (nonatomic, strong) UILabel  *tangleiLabel;
@property (nonatomic, strong) UILabel  *danbeizhiLabel;
@property (nonatomic, strong) UILabel  *zhifangLabel;
@property (nonatomic, strong) UILabel  *countLabel;
@property (nonatomic, strong) UIButton  *subtractBtn;
@property (nonatomic, strong) UIButton  *addBtn;
@property (nonatomic, strong) UIButton  *detailBtn;
@property (nonatomic, strong) YGServerFoodItemModel  *model;
@property (nonatomic, assign) NSInteger  count;

@end
@implementation YGServerItemCollectionViewCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.backgroundColor = UIColorFromRGB(0xf5f5f5);
        [self initView];
        [self initMas];
    }
    return self;
}


- (void)initView{
    [self addSubview:self.coverImageView];
    [self addSubview:self.titleLabel];
    [self addSubview:self.nengliangLabel];
    [self addSubview:self.danbeizhiLabel];
    [self addSubview:self.zhifangLabel];
    [self addSubview:self.tangleiLabel];
    [self addSubview:self.countLabel];
    [self addSubview:self.subtractBtn];
    [self addSubview:self.addBtn];
    [self addSubview:self.detailBtn];
}

- (void)initMas{
    [self.coverImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(10);
        make.width.height.mas_equalTo(50);
        make.left.mas_equalTo(10);
    }];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.coverImageView.mas_bottom).offset(10);
        make.left.mas_equalTo(self.coverImageView);
        make.width.mas_equalTo(100);
    }];
    
    [self.nengliangLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.coverImageView.mas_top);
        make.left.mas_equalTo(self.coverImageView.mas_right).offset(60);
        make.height.mas_equalTo(11);
    }];
    [self.tangleiLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.nengliangLabel.mas_bottom).offset(5);
        make.left.mas_equalTo(self.nengliangLabel.mas_left);
        make.height.mas_equalTo(11);
    }];
    [self.danbeizhiLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.tangleiLabel.mas_bottom).offset(5);
        make.left.mas_equalTo(self.nengliangLabel.mas_left);
        make.height.mas_equalTo(11);
    }];
    [self.zhifangLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.danbeizhiLabel.mas_bottom).offset(5);
        make.left.mas_equalTo(self.nengliangLabel.mas_left);
        make.height.mas_equalTo(11);
    }];
    
    [self.subtractBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.mas_equalTo(22);
        make.top.mas_equalTo(self.nengliangLabel.mas_top);
        make.right.mas_equalTo(self.countLabel.mas_left);
    }];
    
    [self.countLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(22);
        make.width.mas_equalTo(30);
        make.top.mas_equalTo(self.nengliangLabel.mas_top);
        make.right.mas_equalTo(self.addBtn.mas_left);
    }];
    
    [self.addBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.mas_equalTo(22);
        make.top.mas_equalTo(self.nengliangLabel.mas_top);
        make.right.mas_equalTo(self.mas_right).offset(-10);
    }];
    
    [self.detailBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.right.mas_equalTo(-10);
        make.width.height.mas_offset(30);
    }];
}

- (void)configureModel:(YGServerFoodItemModel *)model{
    self.model = model;
    [self.coverImageView sd_setImageWithURL:[NSURL URLWithString:model.thumbnail]];
    self.titleLabel.text = model.typeName;
    self.countLabel.text = [@(model.num) stringValue];
    self.count =  model.num;
    self.nengliangLabel.text = [NSString stringWithFormat:@"能量：%dkcal",model.enerc];
    self.zhifangLabel.text = [NSString stringWithFormat:@"脂肪：%dg",model.fat];
    self.danbeizhiLabel.text = [NSString stringWithFormat:@"蛋白质：%dg",model.procnt];
    self.tangleiLabel.text = [NSString stringWithFormat:@"脂：%dg",model.chocdf];

}

- (void)clickSubBtn{
    if (self.count == 0) {
        return;
    }
    self.count -= 1;
    [self updateCount];
}

- (void)clickAddBtn{
    self.count += 1;
    [self updateCount];
    
}

- (void)clickDetailBtn{
    if ([self.delegate respondsToSelector:@selector(serverCellClickDetail:)]) {
        [self.delegate serverCellClickDetail:self.model];
    }
}

- (void)updateCount{
    self.countLabel.text = [NSString stringWithFormat:@"%d",self.count];
    self.model.num = self.count;
    if ([self.delegate respondsToSelector:@selector(serverCellSelectCount:)]) {
        [self.delegate serverCellSelectCount:self.model];
    }
}
- (UIImageView *)coverImageView{
    if (!_coverImageView) {
        _coverImageView = [[UIImageView alloc] init];
    }
    return _coverImageView;
}

- (UILabel *)nengliangLabel{
    if (!_nengliangLabel) {
        _nengliangLabel = [[UILabel alloc] init];
        _nengliangLabel.textColor = UIColorFromRGB(0x333333);
        _nengliangLabel.font = [UIFont systemFontOfSize:11];
    }
    return _nengliangLabel;
}

- (UILabel *)titleLabel{
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.textColor = UIColorFromRGB(0x333333);
        _titleLabel.font = [UIFont systemFontOfSize:11];
    }
    return _titleLabel;
}

- (UILabel *)danbeizhiLabel{
    if (!_danbeizhiLabel) {
        _danbeizhiLabel = [[UILabel alloc] init];
        _danbeizhiLabel.textColor = UIColorFromRGB(0x333333);
        _danbeizhiLabel.font = [UIFont systemFontOfSize:11];
    }
    return _danbeizhiLabel;
}



- (UILabel *)zhifangLabel{
    if (!_zhifangLabel) {
        _zhifangLabel = [[UILabel alloc] init];
        _zhifangLabel.textColor = UIColorFromRGB(0x333333);
        _zhifangLabel.font = [UIFont systemFontOfSize:11];
    }
    return _zhifangLabel;
}

- (UILabel *)countLabel{
    if (!_countLabel) {
        _countLabel = [[UILabel alloc] init];
        _countLabel.textAlignment = NSTextAlignmentCenter;
        _countLabel.textColor = UIColorFromRGB(0x333333);
        _countLabel.font = [UIFont systemFontOfSize:10];
        _countLabel.layer.borderWidth = 0.5;
        _countLabel.layer.borderColor = [UIColor whiteColor].CGColor;
    }
    return _countLabel;
}

- (UIButton *)subtractBtn{
    if (!_subtractBtn) {
        _subtractBtn = [[UIButton alloc] init];
        [_subtractBtn addTarget:self action:@selector(clickSubBtn) forControlEvents:UIControlEventTouchUpInside];
        [_subtractBtn setTitle:@"-" forState:UIControlStateNormal];
        [_subtractBtn setTitleColor:UIColorFromRGB(0x333333) forState:UIControlStateNormal];
        _subtractBtn.layer.borderWidth = 0.5;
        _subtractBtn.layer.borderColor = [UIColor whiteColor].CGColor;
    }
    return _subtractBtn;
}

- (UILabel *)tangleiLabel{
    if (!_tangleiLabel) {
        _tangleiLabel = [[UILabel alloc] init];
        _tangleiLabel.textColor = UIColorFromRGB(0x333333);
        _tangleiLabel.font = [UIFont systemFontOfSize:10];
    }
    return _tangleiLabel;
}

- (UIButton *)addBtn{
    if (!_addBtn) {
        _addBtn = [[UIButton alloc] init];
        [_addBtn addTarget:self action:@selector(clickAddBtn) forControlEvents:UIControlEventTouchUpInside];
        [_addBtn setTitle:@"+" forState:UIControlStateNormal];
        [_addBtn setTitleColor:UIColorFromRGB(0x333333) forState:UIControlStateNormal];
        _addBtn.layer.borderWidth = 0.5;
        _addBtn.layer.borderColor = [UIColor whiteColor].CGColor;
    }
    return _addBtn;
}

- (UIButton *)detailBtn{
    if (!_detailBtn) {
        _detailBtn = [[UIButton alloc] init];
        [_detailBtn addTarget:self action:@selector(clickDetailBtn) forControlEvents:UIControlEventTouchUpInside];
        [_detailBtn setTitle:@"详情" forState:UIControlStateNormal];
        _detailBtn.titleLabel.textAlignment = NSTextAlignmentRight;
        _detailBtn.titleLabel.font = [UIFont systemFontOfSize:12];
        [_detailBtn setTitleColor:UIColorFromRGB(0x37C8D7) forState:UIControlStateNormal];
    }
    return _detailBtn;
}

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event{

    UIView *view = [super hitTest:point withEvent:event];

    for (UIView *subView in self.subviews) {
        CGPoint convertPoint = [subView convertPoint:point fromView:self];
        if (CGRectContainsPoint(subView.bounds, convertPoint)) {
            if ([subView isKindOfClass:[UIButton class]]) {
                view =  subView;
            }
        }
    }

    return view;
}

@end
